package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.TMenuDao;
import com.ktjy.ktmall.entity.OrderDetail;
import com.ktjy.ktmall.entity.TMenu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TMenuDaoImpl extends BaseDao implements TMenuDao {
    @Override
    public List<TMenu> getMenus() {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        List<TMenu> list = new ArrayList<>();
        String sql = "select * from t_menu ";
        try {
            pstmt = conn.prepareStatement(sql);
//            pstmt.setObject(1, orderId);
            rs = pstmt.executeQuery();
            while (!rs.isClosed() && rs.next()) {
                TMenu od =  new TMenu();
                od.setPsId(rs.getLong("ps_id"));
                od.setPsName(rs.getString("ps_name"));
                od.setPsPid(rs.getLong("ps_pid"));
                od.setPsApiPath(rs.getString("ps_api_path"));
                list.add(od);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }


    /**
     * 查询所有菜单
     * @return
     */
    public List<TMenu> getMenusByIds(Long id){
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        List<TMenu> list = new ArrayList<>();
        String sql = "select * from t_menu  where  ps_pid =? ";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            while (!rs.isClosed() && rs.next()) {
                TMenu od =  new TMenu();
                od.setPsId(rs.getLong("ps_id"));
                od.setPsName(rs.getString("ps_name"));
                od.setPsPid(rs.getLong("ps_pid"));
                od.setPsApiPath(rs.getString("ps_api_path"));
                list.add(od);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }
}
